package com.google.sydym6.view.activities;

import com.google.sydym6.R;
import com.google.sydym6.controllers.IPlayRadioController;
import com.google.sydym6.controllers.PlayRadioController;
import com.google.sydym6.logic.IRadioScrobblerModel;
import com.google.sydym6.logic.RadioScrobblerModel;
import com.google.sydym6.logic.managers.RadioManager;
import com.google.sydym6.logic.observers.IPlayerStateObservable;
import com.google.sydym6.logic.observers.IPlayerStateObserver;
import com.google.sydym6.logic.observers.IRecognizeStatusObservable;
import com.google.sydym6.logic.observers.IRecognizeStatusObserver;
import com.google.sydym6.view.adapters.RadioPlayerPagerAdapter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class RadioPlayerActivity extends AbstractMiniPlayerActivity {

	  @Override
	  protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.radio_player_layout);
	    
	    model = RadioScrobblerModel.getInstance();
	    
	    ImageButton playPauseButton = (ImageButton) findViewById(R.id.playPauseButton);
	    playPauseButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				System.out.println("playPauseButton was clicked");
				
				controller.playStopRadio();
			}
		});
	    
	    LinearLayout miniplayer = (LinearLayout) findViewById(R.id.radioMiniPlayer);
	    miniplayer.setVisibility(View.VISIBLE);
	    
	    pager = (ViewPager) findViewById(R.id.radioPlayerPager); 
			
	    Log.v("Multitreading", "RadioPlayerActivity: Creating new observers");
	    IRecognizeStatusObservable radioManagerStatusObservable = (IRecognizeStatusObservable) model.getRadioManager();
		radioManagerStatusObservable.addObserver((IRecognizeStatusObserver)this);
		
		IPlayerStateObservable radioManagerStateObservable = (IPlayerStateObservable) model.getRadioManager();
		radioManagerStateObservable.addObserver((IPlayerStateObserver)this);
	  }
	  
	  @Override
	  protected void onResume() {
		  super.onResume();
		  
		  controller = new PlayRadioController(this);
		    
		  pagerAdapter = new RadioPlayerPagerAdapter(getSupportFragmentManager());
		  pager.setAdapter(pagerAdapter);

		  pager.setOnPageChangeListener(new OnPageChangeListener() {

			  @Override
			  public void onPageSelected(int position) {
				  Log.d(TAG, "onPageSelected, position = " + position);
			  }

			  @Override
			  public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
			  }

			  @Override
			  public void onPageScrollStateChanged(int state) {
			  }
		  });
		   
		  updatePlayerState();
		  updateRecognizeStatus();
		  setRadioName();
	  }
	  
	  @Override
	  protected void onDestroy() {
		  super.onDestroy();
		  
		  Log.v("Multitreading", "RadioPlayerActivity: Deleting observers");
		  IRecognizeStatusObservable radioManagerStatusObservable = (IRecognizeStatusObservable) model.getRadioManager();
		  radioManagerStatusObservable.removeObserver((IRecognizeStatusObserver)this);
			
		  IPlayerStateObservable radioManagerStateObservable = (IPlayerStateObservable) model.getRadioManager();
		  radioManagerStateObservable.removeObserver((IPlayerStateObserver)this);
	  }
}
